#include "qruntimecheckdialog.h"
#include "ui_qruntimecheckdialog.h"

#include "../../libs/core/qbaseitemdelegate.h"
#include "../../libs/core/qsoftcore.h"
#include "../../libs/core/stylehelper.h"
#include "../../libs/machine/qmachine.h"
#include "../../libs/machine/qmachinevalue.h"

QRuntimeCheckDialog::QRuntimeCheckDialog(QWidget *parent) :
    QBaseDialog(parent),
    ui(new Ui::QRuntimeCheckDialog),
    m_ret(0)
{
    ui->setupUi(this);

    ui->treeWidget->setItemDelegate(new QBaseItemDelegate);
    ui->treeWidget->setRootIsDecorated(false);
    ui->treeWidget->setAlternatingRowColors(true);

    QMachine* machine=QSoftCore::get_core()->get_machine();

    QList<QMachineValue*> list=machine->get_all_runtimes();

    m_none_icon=StyleHelper::drawCheckbox(this->palette(),this->style(),0);
    m_all_icon=StyleHelper::drawCheckbox(this->palette(),this->style(),1);
    m_some_icon=StyleHelper::drawCheckbox(this->palette(),this->style(),2);

    foreach(QMachineValue* value,list)
    {
        QTreeWidgetItem *item=new QTreeWidgetItem(ui->treeWidget);
        item->setText(0,value->get_value_property(EPI_CODE).toString()+":"
                      +value->get_value_property(EPI_NAME).toString());
        item->setToolTip(0,item->text(0));
        item->setIcon(0,m_none_icon);
        item->setData(0,Qt::UserRole,0);
        m_item_to_value.insert(item,value);
        m_value_to_item.insert(value,item);
    }

    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(item_clicked(QTreeWidgetItem*)));
}

QRuntimeCheckDialog::~QRuntimeCheckDialog()
{
    delete ui;
}

void QRuntimeCheckDialog::show_runtime(QList<QMachineValue *> list)
{
    foreach(QMachineValue* value,list)
    {
        QTreeWidgetItem *item=m_value_to_item.value(value);
        if(item!=NULL)
        {
            item->setIcon(0,m_all_icon);
            item->setData(0,Qt::UserRole,2);
        }
    }
    exec();
}

QList<QMachineValue*>   QRuntimeCheckDialog::get_runtime()
{
    QList<QMachineValue*>   list;

    int count=ui->treeWidget->topLevelItemCount();
    for(int i=0;i<count;i++)
    {
        QTreeWidgetItem *item=ui->treeWidget->topLevelItem(i);
        if(item->data(0,Qt::UserRole).toInt()==2)
        {
            list.append(m_item_to_value.value(item));
        }
    }
    return list;
}

void QRuntimeCheckDialog::on_cancel_clicked()
{
    close();
}

void QRuntimeCheckDialog::on_ok_clicked()
{
    m_ret=1;
    close();
}

void QRuntimeCheckDialog::on_back_clicked()
{
    int count=ui->treeWidget->topLevelItemCount();
    for(int i=0;i<count;i++)
    {
        QTreeWidgetItem *item=ui->treeWidget->topLevelItem(i);
        if(item->data(0,Qt::UserRole).toInt()==2)
        {
            item->setIcon(0,m_none_icon);
            item->setData(0,Qt::UserRole,0);
        }
        else
        {
            item->setIcon(0,m_all_icon);
            item->setData(0,Qt::UserRole,2);
        }
    }
}

void QRuntimeCheckDialog::on_select_all_clicked()
{
    int count=ui->treeWidget->topLevelItemCount();
    for(int i=0;i<count;i++)
    {
        QTreeWidgetItem *item=ui->treeWidget->topLevelItem(i);
        item->setIcon(0,m_all_icon);
        item->setData(0,Qt::UserRole,2);
    }
}

void QRuntimeCheckDialog::item_clicked(QTreeWidgetItem *item)
{
    if(item->data(0,Qt::UserRole).toInt()==2)
    {
        item->setIcon(0,m_none_icon);
        item->setData(0,Qt::UserRole,0);
    }
    else
    {
        item->setIcon(0,m_all_icon);
        item->setData(0,Qt::UserRole,2);
    }
}

int QRuntimeCheckDialog::get_ret()
{
    return m_ret;
}
